home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / mail / mh / vmail / vmail.2of3 / main.c < prev    next >
C/C++ Source or Header  |  1991-04-05  |  10KB  |  363 lines

  1. #ifndef lint
  2. static char *RCS_main_c = "$Id: main.c,v 1.4 91/03/08 15:57:32 jamesp Exp $";
  3. #endif
  4.  
  5. /* --------------------
  6.     vmail -- main.c
  7.  
  8.     Definitions of global variables, main switch.
  9.  
  10.     Copyright (C) J. Zobel, University of Melbourne, October 1987.
  11. -------------------- */
  12.  
  13. #include "defs.h"
  14.  
  15. folder    folders = (folder) NULL, curflr, alternate = (folder) NULL;
  16. item    curmail;
  17. char     *user, *pager = (char *) NULL, *editor = (char *) NULL,
  18.         *mail_dir = (char *) NULL, *shell = (char *) NULL, *context;
  19. bool    top_level = true, do_flush = true, comp_args = true, repl_args = true,
  20.         forw_args = true, sort_args = true, burst_args = true,
  21.         dist_args = true;
  22. int        lines, cols, y, folder_protect = FPROT;
  23. jmp_buf    env;
  24.  
  25. #define HELP        (sizeof(help_scr) / sizeof(*help_scr))
  26.  
  27. char *help_scr[] = {
  28. "       .                Re-execute last command (if one of acdDefirR)",
  29. "                                   (on repeat, r does not prompt for folder)",
  30. "       /<exp>           Search forwards for title with <exp> (/ to repeat)",
  31. "       ?<exp>           Search backwards for title with <exp> (? to repeat)",
  32. "       <space>          Show current mail item",
  33. "       [n]<return>      Go forward one (or n) active page(s) of mail items",
  34. "       [n]<backspace>   Go back one (or n) active page(s) of mail items",
  35. "       ^L               Refresh",
  36. "       ^R               Refresh item listing, to match current mh folder",
  37. "       !                Invoke shell",
  38. "       |                Pipe current mail item into command",
  39. "       ^                Go to first active page",
  40. "       $                Go to last active page",
  41. "       a                Answer current mail item (call to \"repl\")",
  42. "       b                Burst current mail item as a digest",
  43. "       c                Compose new mail item (call to \"comp\")",
  44. "       C                Go to folder chooser",
  45. "       [n]d             Delete current (or next inclusive n) mail item(s)",
  46. "       D                Delete current mail item, show next",
  47. "       e                Edit current mail item",
  48. "       f                Forward current mail item (call to \"forw\")",
  49. "       F                List all folders",
  50. "       g,G              Go to named (or alternate) folder",
  51. "       h                Show this page",
  52. "       [n]H             Go to top of page (or nth from top line)",
  53. "       i                Incorporate new mail (call to \"inc\")",
  54. "       [n]j             Move cursor down one (or n) line(s)",
  55. "       [n]k             Move cursor up one (or n) line(s)",
  56. "       [n]L             Go to bottom of page (or nth from bottom line)",
  57. "       M                Go to middle of page",
  58. "       [n]n             Go forward one (or n) folders, make new folder active",
  59. "       [n]p             Go back one (or n) folders, make new folder active",
  60. "       P                Print name of alternate folder",
  61. "       q                Exit from vmail",
  62. "       r,R              Refile item in named (or previous) folder",
  63. "       s                Save current item in named file",
  64. "       S                Sort mail items by date",
  65. "       t                Distribute mail item to new recipients (calls 'dist')",
  66. "       u                Undo most recent deletion (1 mail item only)",
  67. "       v                Make current folder inactive",
  68. "       z                Pack current folder"
  69. };
  70.  
  71.  
  72. #define USAGE    (void)printf( \
  73. "Usage: vmail [-inc] [-flush] [-comp] [-forw] [-ans] [+curfolder] folders ...\n"), \
  74.                 exit(1)
  75.  
  76. main(argc, argv)
  77.     int        argc;
  78.     char    *argv[];
  79. {
  80.     char    c, last = '\0', get_number(), flushin();
  81.     int        count = 1,        /* accumulate count to give to command */
  82.             pcount = 1;
  83.     bool    flag = false;    /* used to check whether to reset count */
  84.  
  85.     argc--, argv++;
  86.  
  87.     process_args(argc, argv);
  88. #ifndef VERSION
  89. #define VERSION    "(local version)"
  90. #endif
  91.     (void)printf("vmail %s -- reading mail headers\n", VERSION);
  92.     init(argc, argv);
  93.     (void)setjmp(env);        /* return point from interrupt */
  94.     c = flushin();
  95.     for(;;) {
  96.         while(c == '.')
  97.             if(last == '\0') {
  98.                 beep();
  99.                 c = flushin();
  100.             } else {
  101.                 count = pcount;
  102.                 c = last;
  103.             }
  104.         switch(c) {
  105.             case '|':    /* pipe mail to command */
  106.                 do_pipe();
  107.                 break;
  108.             case '!':    /* call shell */
  109.                 call_shell();
  110.                 break;
  111.             case '/':    /* search forwards */
  112.                 search(true);
  113.                 break;
  114.             case '?':    /* search backwards */
  115.                 search(false);
  116.                 break;
  117.             case '^':    /* first active page */
  118.                 goto_first_page();
  119.                 break;
  120.             case '$':    /* last active page */
  121.                 goto_last_page();
  122.                 break;
  123.             case '\n':    /* next folder */
  124.             case '\r':    /* next folder */
  125.                 next_page(count, true);
  126.                 break;
  127.             case ' ':    /* show current item */
  128.                 show_mail();
  129.                 break;
  130.             case DEL:    /* previous folder */
  131.             case '\b':    /* previous folder */
  132.                 prev_page(count, true);
  133.                 break;
  134.             case 'a':    /* answer - call to repl */
  135.                 repl();
  136.                 break;
  137.             case 'b':    /* burst current mail item */
  138.                 burst_item();
  139.                 break;
  140.             case 'c':    /* compose - fork of comp */
  141.                 comp();
  142.                 break;
  143.             case 'C':    /* go to folder chooser */
  144.                 choose();
  145.                 break;
  146.             case 'd':    /* delete item */
  147.                 delete_item(count);
  148.                 break;
  149.             case 'D':    /* delete item, show next */
  150.                 (void)change_item(false);
  151.                 show_mail();
  152.                 break;
  153.             case 'e':    /* edit current mail item */
  154.                 edit();
  155.                 break;
  156.             case 'f':    /* forward - call to forw */
  157.                 forw();
  158.                 break;
  159.             case 'F':    /* list all folders */
  160.                 list_folders();
  161.                 break;
  162.             case 'g':    /* go to named folder */
  163.                 goto_folder(true);
  164.                 break;
  165.             case 'G':    /* go to last-named folder */
  166.                 goto_folder(false);
  167.                 break;
  168.             case 'h':    /* help */
  169.                 help();
  170.                 break;
  171.             case 'H':    /* go to top of page */
  172.                 cursor_first(count);
  173.                 break;
  174.             case 'i':    /* inc */
  175.                 inc();
  176.                 break;
  177.             case 'j':    /* cursor down */
  178.                 cursor_down(count);
  179.                 break;
  180.             case 'k':    /* cursor up */
  181.                 cursor_up(count);
  182.                 break;
  183.             case CTRL_L: /* redraw */
  184.                 display_page();
  185.                 break;
  186.             case 'L':    /* go to bottom of page */
  187.                 cursor_last(count);
  188.                 break;
  189.             case 'M':    /* go to middle of page */
  190.                 cursor_middle();
  191.                 break;
  192.             case 'n':    /* next folder - load if not there */
  193.                 goto_next_folder(count);
  194.                 break;
  195.             case 'p':    /* prev folder - load if not there */
  196.                 goto_prev_folder(count);
  197.                 break;
  198.             case 'P':    /* print name of last-named folder */
  199.                 show_folder();
  200.                 break;
  201.             case 'q':    /* quit */
  202.                 to_normal();
  203.                 fix_mh();
  204.                 exit(0);
  205.                 break;
  206.             case 'r':    /* move item to named folder */
  207.                 move_item(true);
  208.                 break;
  209.             case CTRL_R:    /* reread current folder */
  210.                 refresh_folder();
  211.                 break;
  212.             case 'R':    /* move item to previous folder */
  213.                 move_item(false);
  214.                 break;
  215.             case 's':    /* save item in named file */
  216.                 save_item();
  217.                 break;
  218.             case 'S':    /* sort current folder */
  219.                 sort_folder();
  220.                 break;
  221.             case 't':    /* dist a mail item to new people */
  222.                 dist_item();
  223.                 break;
  224.             case 'u':    /* undo */
  225.                 undo();
  226.                 break;
  227.             case 'v':    /* make folder inactive */
  228.                 inactive();
  229.                 break;
  230.             case 'z':    /* pack current folder */
  231.                 pack_folder();
  232.                 break;
  233.             case '0':    /* start of count */
  234.             case '1':
  235.             case '2':
  236.             case '3':
  237.             case '4':
  238.             case '5':
  239.             case '6':
  240.             case '7':
  241.             case '8':
  242.             case '9':
  243.                 c = get_number(c, &count);
  244.                 flag = true;
  245.                 break;
  246.             default:
  247.                 addstatus("command unknown -- `h' for help", true);
  248.                 break;
  249.         }
  250.         switch(c) {
  251.                 /* repeatable commands */
  252.             case 'r':    /* refile item in named folder */
  253.                 last = 'R';
  254.                 pcount = 1;
  255.                 break;
  256.             case 'a':    /* reply - call to repl */
  257.             case 'c':    /* compose - fork of comp */
  258.             case 'd':    /* delete item */
  259.             case 'D':    /* delete item, show next */
  260.             case 'e':    /* edit current mail item */
  261.             case 'f':    /* forward - call to forw */
  262.             case 'i':    /* inc */
  263.             case 'R':    /* refile to previous folder */
  264.             case 't':    /* redistribute - call to dist */
  265.             case 'w':    /* write to file */
  266.                 last = c;
  267.                 pcount = count;
  268.                 break;
  269.             default:
  270.                 break;
  271.         }
  272.         if(!flag) {
  273.             c = flushin();
  274.             count = 1;
  275.         } else {
  276.             flag = false;
  277.         }
  278.     }
  279. }
  280.  
  281.  
  282. /* --------------------
  283.     Read a number from terminal.
  284. -------------------- */
  285. char
  286. get_number(c, count)
  287.     char    c;
  288.     int        *count;
  289. {
  290.     *count = c - '0';
  291.     while((c = getchar()) >= '0' && c <= '9')
  292.         *count = *count * 10 + c - '0';
  293.     return(c);
  294. }
  295.  
  296.  
  297. WINDOW *helpwin = (WINDOW *) NULL;
  298.  
  299. /* --------------------
  300.     Display help messages on help screen.
  301. -------------------- */
  302. help()
  303. {
  304.     WINDOW    *newwin();
  305.     int        i, j;
  306.  
  307.     if(helpwin == (WINDOW *) NULL)
  308.         helpwin = newwin(0, 0, 0, 0);
  309.     (void)wclear(helpwin);
  310.     for(j=i=0 ; i <= HELP ; i++, j++) {
  311.         if((i+2) % lines == 0) {
  312.             if(use_prompt(helpwin) == 'q')
  313.                 break;
  314.             j=0;
  315.             (void)wclear(helpwin);
  316.         }
  317.         if(i >= HELP) {
  318.             (void)use_prompt(helpwin);
  319.             break;
  320.         }
  321.         mvwaddstr(helpwin, j+1, 0, help_scr[i]);
  322.     }
  323.     display_page();
  324. }
  325.  
  326.  
  327. /* --------------------
  328.     Process arguments, command-line or given in profile.
  329. -------------------- */
  330. void
  331. process_args(argc, argv)
  332.     int argc;
  333.     char *argv[];
  334. {
  335.     for( ; argc > 0 ; argc--, argv++)
  336.         if(!strcmp(argv[0], "-inc")) {    /* incorporate mail before starting */
  337.             if(!vfork()) {
  338.                 execlp(INC, INC, 0);
  339.                 (void)printf("Warning: can't execute %s\n", INC);
  340.                 exit(0);
  341.             }
  342.             (void)wait((union wait *)0);
  343.             (void)printf("\n");
  344.         } else if(!strcmp(argv[0], "-flush"))    /* don't flush typeahead */
  345.             do_flush = false;
  346.         else if(!strcmp(argv[0], "-forw"))        /* no args to forw */
  347.             forw_args = false;
  348.         else if(!strcmp(argv[0], "-comp"))        /* no args to comp */
  349.             comp_args = false;
  350.         else if(!strcmp(argv[0], "-ans"))        /* no args to repl */
  351.             repl_args = false;
  352.         else if(!strcmp(argv[0], "-burst"))        /* no args to burst */
  353.             burst_args = false;
  354.         else if(!strcmp(argv[0], "-dist"))        /* no args to dist */
  355.             dist_args = false;
  356.         else if(!strcmp(argv[0], "-sort"))        /* no args to sort */
  357.             sort_args = false;
  358.         else if(*argv[0] == '-') {
  359.             (void)printf("%s: illegal option\n", argv[0]);
  360.             USAGE;
  361.         }
  362. }
  363.